import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LKQ
 * @date 2022/3/6 15:55
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> map1 = new HashMap<>();
        Map<String, Integer> map2 = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            map1.put(list1[i], i);
        }
        for (int i = 0; i < list2.length; i++) {
            map2.put(list2[i], i);
        }
        List<int[]> list = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        int m = 0, n = 0;
        for (String st: map1.keySet()) {
            m = map1.get(st);
            if (map2.containsKey(st)) {
                n = map2.get(st);
                if (m + n < min) {
                    min = m + n;
                    list.add(new int[] {m, n});
                }
            }
        }
        List<String> ans = new ArrayList<>();
        for(int[] s: list) {
            if (s[0] + s[1] == min) {
                ans.add(list1[s[0]])  ;
            }
        }
        return ans.toArray(new String[]{});
    }
}
